package com.yc.project2_knn;

import com.yc.project2_knn.bean.BankMarketing;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据加载器:读取数据文件data和测试文件test
 */
public class BankMarketingLoaader {
    public static List<BankMarketing> load(String path){
        List<BankMarketing> list = new ArrayList<BankMarketing>();
        //TODO:读取path的位置文件，并解析成 Bankmarketing对象
        //技术：java io流  File类   InputStream  二进制流
        //            按一行一行的读取 => 利用InputStreamReader   包装成Reader =>  BufferedReader(装饰模式)  .readLine()

        //2.考虑线程安全的问题，采用线程安全的文件操作类
        //BIO     BufferedReader reader= new BufferedReader( new InputStreamReader( new FileInputStream( path ) ) ) ;  线程不安全的阻塞流
        //要用线程安全的流  new FileInputStream( path ) => InputStream iis= Files . newInputStream( path );

        //3.异常处理 try..。catch    finally
        //转成jdk8以后 try..。resource.。..
        Path p =Paths.get(path);  //以前是new File  现在new path 得到的是一个Path
            //括号里的内容实现Closeable接口  那么可以加入try的括号后面  会自动关闭
        try (InputStream iis = Files.newInputStream(p);
             BufferedReader reader = new BufferedReader(new InputStreamReader(iis));
        ) {  //括号中加入的是可以close的资源   //The stream will be safe for access by multiple concurrent threads.
            String line= null;
            while( (line =reader.readLine())!=null){
                String data[] = line.split(";");
                BankMarketing bank = new BankMarketing();
                bank.setData(data);
                list.add(bank);
            }
        } catch (Exception e) {

        }
        return  list;
    }


}
